package com.kejizhentan.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kejizhentan.common.exception.YyghException;
import com.kejizhentan.common.result.Result;
import com.kejizhentan.common.utils.MD5;
import com.kejizhentan.service.HospitalSetService;
import com.kejizhentan.yygh.model.hosp.HospitalSet;
import com.kejizhentan.yygh.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javafx.scene.chart.ScatterChart;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;
/**
 * @Auther: kejizhentan
 * @Date 2022/11/20 20:59
 * @Description: 医院设置接口crud
 */
@Api(tags="医院设置的相关接口")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
//@CrossOrigin
public class HospitalSetController {
    @Autowired
    private HospitalSetService hospitalSetService;

    @ApiOperation("查询所有医院设置")
    @GetMapping("/findAll")
    public Result findAllHospitalSet(){
        return Result.ok(hospitalSetService.list());
    }

    @ApiOperation("删除医院设置")
    @DeleteMapping("{id}")
    public Result deleteHospitalSet(@PathVariable Long id){
        boolean flag = hospitalSetService.removeById(id);
        if(flag){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    /**
     * 条件查询医院信息带分页
     */
    @ApiOperation(value = "条件查询医院设置信息带分页")
    @PostMapping("/findPageHospSet/{current}/{limit}")
    public Result findPageHospSet(@PathVariable long current,
                                  @PathVariable long limit,
                                  @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo) {

        // 创建 page 对象，传递当前页和每页记录数
        Page<HospitalSet> page = new Page<>(current, limit);
        // 构建条件
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
        String hosname = hospitalSetQueryVo.getHosname();// 医院名称
        String hoscode = hospitalSetQueryVo.getHoscode();// 医院编号

        if (!StringUtils.isEmpty(hosname)) {
            wrapper.like("hosname", hospitalSetQueryVo.getHosname());
        }
        if (!StringUtils.isEmpty(hoscode)) {
            wrapper.eq("hoscode", hospitalSetQueryVo.getHoscode());
        }

        // 调用方法实现分页查询
        Page<HospitalSet> pageHospitalSet = hospitalSetService.page(page, wrapper);

        return Result.ok(pageHospitalSet);
    }


    /**
     * 添加医院设置
     */
    @ApiOperation(value = "添加医院设置")
    @PostMapping("/saveHospitalSet")
    public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet) {
        // 设置状态 1：使用 0：不能使用
        hospitalSet.setStatus(1);
        // 签名密钥
        Random random = new Random();
        String key = MD5.encrypt(System.currentTimeMillis() + "" + random.nextInt(1000));
        hospitalSet.setSignKey(key);
        // 调用 service
        return hospitalSetService.save(hospitalSet) ? Result.ok() : Result.fail();
    }

    /**
     * 根据 id 获取医院设置
     */
    @ApiOperation(value = "根据 id 获取医院设置")
    @GetMapping("/getHospSet/{id}")
    public Result getHospSet(@PathVariable long id) {
        return Result.ok(hospitalSetService.getById(id));
    }


    /**
     * 修改医院设置
     */
    @ApiOperation(value = "修改医院设置")
    @PostMapping("/updateHospitalSet")
    public Result updateHospitalSet(@RequestBody HospitalSet hospitalSet) {
        return hospitalSetService.updateById(hospitalSet) ? Result.ok() : Result.fail();
    }

    /**
     * 批量删除医院设置
     */
    @ApiOperation(value = "批量删除医院设置")
    @DeleteMapping("/batchRemove")
    public Result batchRemoveHospitalSet(@RequestBody List<Long> idList) {
        return hospitalSetService.removeByIds(idList) ? Result.ok() : Result.fail();
    }

    /**
     * 医院设置锁定和解锁
     */
    @ApiOperation(value = "医院设置锁定和解锁")
    @PutMapping("/lockHospitalSet/{id}/{status}")
    public Result lockHospitalSet(@PathVariable Long id,
                                  @PathVariable Integer status) {
        // 根据 id 查询医院信息
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        // 设置状态
        hospitalSet.setStatus(status);
        // 更新状态
        boolean flag = hospitalSetService.updateById(hospitalSet);
        return flag ? Result.ok() : Result.fail();
    }
}
